/**
 * @file   test_mutex010.c
 * @brief	Testing barging
 * Expected result: 12345679 because the system task is prevented from barging the 2nd BRR task.
 * Note that to test barging, the BRR should not be promoted to System level
 * (That is, the line that promotes the BRR task to System level should be commented out for this testing.)
 */
#include "../header/common.h"
#include "../header/os.h"
#include "../header/uart.h"
#include "../header/trace.h"
#include <util/delay.h>
#ifdef test_mutex010

const unsigned char PPP[] = {};
const unsigned int PT = 0;
EVENT* print_event;
MUTEX* mutex;
int flag = 0;

void another_task(void) {
	add_to_trace(2);
	Mutex_Lock(mutex, 1);
	add_to_trace(5);
	Mutex_Unlock(mutex);
}

void system_task(void) {
	add_to_trace(4);
	Mutex_Lock(mutex, 1);
	add_to_trace(6);
	Mutex_Unlock(mutex);
}

void generic_task(void)
{
	add_to_trace(1);
	Mutex_Lock(mutex, 1);
    //Task_Create(another_task, 1, BRR, 0);
    Task_Next();
	add_to_trace(3);
	Task_Create(system_task,2,SYSTEM,1);
	Mutex_Unlock(mutex);
	Task_Next();
	add_to_trace(7);
	Event_Signal(print_event);
}

int main(void) {
	//use pins 4,5,6 and 7 on Port D for output
	DDRD = (1<<PORTD4) | (1<<PORTD5) | (1<<PORTD6) | (1<<PORTD7);
	UART_BPS bitrate = UART_38400;
	uart_init(bitrate);

    print_event = Event_Init();
    mutex = Mutex_Init();

    Task_Create(generic_task, 0, BRR, 0);
    Task_Create(another_task, 1, BRR, 0);

    Event_Wait(print_event);
    add_to_trace(9);
    print_trace();

    return 0;
}

#endif
